package window.component;

import entity.HotelInfo;
import window.frame.HotelDetailFrame;
import window.tab.HotelTab;

import javax.swing.*;
import java.awt.*;

public class HotelCard extends JPanel {
    HotelTab hotelTab;

    public HotelCard(HotelInfo hotel, HotelTab hotelTab) {
        this.hotelTab = hotelTab;
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));


        ImageIcon hotelImage = new ImageIcon(hotel.getImgUrl());
        Image originalImage = hotelImage.getImage();
        Image scaledImage = originalImage.getScaledInstance(150, 100, Image.SCALE_SMOOTH);
        hotelImage = new ImageIcon(scaledImage);
        JLabel imageLabel = new JLabel(hotelImage);

        add(imageLabel, BorderLayout.WEST);


        JPanel infoPanel = new JPanel();
        infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
        infoPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));


        JLabel nameLabel = new JLabel(hotel.getName());
        nameLabel.setFont(new Font("Arial", Font.BOLD, 20));
        infoPanel.add(nameLabel);


        JLabel ratingLabel = new JLabel(hotel.getRating() + "/5 stars");
        ratingLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        ratingLabel.setForeground(Color.GRAY);
        infoPanel.add(ratingLabel);


        JLabel locationLabel = new JLabel(hotel.getAddress());
        locationLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        infoPanel.add(locationLabel);


        JLabel priceLabel = new JLabel("RMB " + hotel.getPrice());
        priceLabel.setFont(new Font("Arial", Font.BOLD, 16));
        priceLabel.setForeground(Color.red);
        infoPanel.add(priceLabel);


        JLabel roomInfoLabel = new JLabel(hotel.getRoom() + " rooms available");
        roomInfoLabel.setFont(new Font("Arial", Font.PLAIN, 14));
        infoPanel.add(roomInfoLabel);


        JButton checkRoomButton = new JButton("Book Now");
        checkRoomButton.setBackground(new Color(0, 123, 255)); // 蓝色按钮
        checkRoomButton.setForeground(Color.WHITE);
        this.add(checkRoomButton, BorderLayout.SOUTH);
        checkRoomButton.addActionListener(e -> {
            if (hotel.getRoom() > 0) {
                HotelDetailFrame hotelDetailFrame = new HotelDetailFrame(hotel, hotelTab);
                hotelDetailFrame.setVisible(true);
                System.out.println("Book Now button clicked, navigating to booking details for hotel: " + hotel.getName());
            } else {
                JOptionPane.showMessageDialog(null,
                        "The rooms are all booked",
                        "Booking Status",
                        JOptionPane.INFORMATION_MESSAGE);
            }
        });

        add(infoPanel, BorderLayout.CENTER);
        this.setPreferredSize(new Dimension(450, 140));
    }
}

